package com.apes.framework.rop.securityManager;

import com.apes.framework.plugin.cache.Cache;
import com.apes.framework.rop.session.SimpleSession;
import com.apes.framework.rop.session.store.SessionService;
import com.apes.oap.session.Session;
import com.apes.oap.session.SessionManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

public class SampleSessionManager implements SessionManager {
    protected final Logger logger = LogManager.getLogger(SampleSessionManager.class);

    private final String CONST_DEFAULT_CACHE_NAME = "sessionCache";

    @Autowired
    private Cache cache;

    @Autowired
    private SessionService sessionService;

    public void addSession(String sessionId, Session session) {
        cache.put(CONST_DEFAULT_CACHE_NAME, sessionId, session);
        sessionService.save((SimpleSession) session);
    }

    public Session getSession(String sessionId) {
        return (Session) cache.get(CONST_DEFAULT_CACHE_NAME, sessionId);
    }

    public void removeSession(String sessionId) {
        cache.remove(CONST_DEFAULT_CACHE_NAME, sessionId);
        sessionService.update(sessionId);
    }
}
